package com.myself.community.service.impl;

import com.myself.community.service.LikeService;
import com.myself.community.utils.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 点赞
     * @param userId
     * @param entityType
     * @param entityId
     * @param entityUserId
     */
    @Override
    public void like(int userId, int entityType, int entityId, int entityUserId) {
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                //存储赞的key
                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
                //被点赞实体的作者的总共赞数量的key：like:user:userId
                String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);

                //判断存储赞的key中是否已经存有用户id：即当前用户已对其点过赞
                boolean isMember = operations.opsForSet().isMember(entityLikeKey, userId);

                //开启Redis事务
                operations.multi();

                if (isMember) {
                    //点过赞了：则执行取消赞，即删除对应的key中存放的用户id
                    operations.opsForSet().remove(entityLikeKey, userId);
                    //实体作者的点赞总数自增1
                    operations.opsForValue().decrement(userLikeKey);
                } else {
                    //没有点赞：执行点赞
                    operations.opsForSet().add(entityLikeKey, userId);
                    //实体作者的点赞总数减1
                    operations.opsForValue().increment(userLikeKey);
                }

                //提交事务
                return operations.exec();
            }
        });
    }

    /*
    查询某实体点赞的数量
     */
    @Override
    public long findEntityLikeCount(int entityType, int entityId) {
        //此实体赞的key：like:entity:entityType:entityId
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        //查询此实体的点赞key的数量
        return redisTemplate.opsForSet().size(entityLikeKey);
    }

    /*
    查询某人对某实体的点赞状态
     */
    @Override
    public int findEntityLikeStatus(int userId, int entityType, int entityId) {
        //此实体赞的key：like:entity:entityType:entityId
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        //判断这个key中是否已经存有用户id：即当前用户已对其点过赞
        return redisTemplate.opsForSet().isMember(entityLikeKey, userId) ? 1 : 0;
    }

    /*
    查询某个用户获得的赞的数量
     */
    @Override
    public int findUserLikeCount(int userId) {
        //得到key：like:user:userId
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        //获取对应key的值：即为传入用户点赞总数
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        return count == null ? 0 : count.intValue();
    }
}
